Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLike: true,
    detail: {},
    pjDataList: [{
      headpic: '/assets/images/avatar.png',
      author: '张三',
      add_time: '2018-06-01',
      content: '好评好评，真实太好了!'
    },
    {
      headpic: '/assets/images/avatar.png',
      author: '张三',
      add_time: '2018-06-01',
      content: '好评好评，真实太好了!'
    }
    ], //评价数据
  },
  // 收藏
  addLike() {
    const app = getApp();
    // 收藏
    if (this.data.isLike) {
      app.globalData.collections.push(this.data.detail)
    } else {
      // 取消收藏
      const collections = app.globalData.collections;
      app.globalData.collections.splice(collections.findIndex(c => c.id === this.data.detail.id), 1)
    }
    this.setData({
      isLike: !this.data.isLike
    });
  },

  // 立即购买
  immeBuy() {
    const app = getApp();
    app.globalData.orders.push({
      ...this.data.detail,
      status: 1,
      count: {
        quantity: 1,
        min: 1,
        max: 20
      }
    })
    wx.showToast({
      title: '购买成功',
      icon: 'success',
      duration: 2000
    });
  },
  // 购物车
  toCar() {
    wx.switchTab({
      url: '/pages/cart/cart'
    })
  },
  addCar() {
    const app = getApp()
    let found = false;
    app.globalData.cartList.forEach(cart => {
      if (cart.id === this.data.detail.id) {
        cart.count.quantity += 1;
        found = true;
      }
    })
    if (!found) {
      app.globalData.cartList.push({
        ...this.data.detail,
        count: {
          quantity: 1,
          min: 1,
          max: 20
        }
      })
    }

    wx.showToast({
      title: '加入成功',
      icon: 'success',
      duration: 2000
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this
    this.getOpenerEventChannel().on('entry', function (data) {
      (data)
      that.setData({
        detail: data
      })
    })
  },
})